<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>发送通知</title>
    <script type="text/javascript" src="../../static/common/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../../static/common/jquery/jquery-1.11.1.min.js"></script>
    <link rel="stylesheet" href="../../static/common/layui/css/layui.css">
    <script src="../../static/common/axios.min.js"></script>
    <script src="../../static/common/vue.min.js"></script>
</head>
<script>
    var userId = ${sessionScope.currentUser.getId()};
    var positionTypes = [];//所有岗位信息
    var orgId;
        axios.get('/orginfo/findByManagerId/'+ userId)
        .then(function (response) {
            console.log(response.data);
            orgId=response.data.data.orgInfo.id;
        })
        .catch(function (response) {
            console.log(response);
        });

    //获取全部岗位信息
    axios.get('/PositionType/findAll')
        .then(function (response) {
            for (var i = 0;
                 i < response.data.data.length; i++) {
                positionTypes[response.data.data[i].id] = response.data.data[i].posname;
            }
//            console.log(positionTypes);
            //有效岗位类别
            var validPositionTypes = response.data.data;
            var html = '';
            for (var i in validPositionTypes) {
                html += '<option value=' + validPositionTypes[i].id + '>' + validPositionTypes[i].posname + '</option>';
            }
            $('select[name=positiontype]').append(html);
        })
        .catch(function (response) {
            console.log(response);
        });
</script>
<body>
<div style="margin: 15px;">
    <form class="layui-form" action="">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>招聘信息</legend>
        </fieldset>
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input name="title" id="title" class="layui-input">
                </div>
                <label class="layui-form-label">招聘类别</label>
                <div class="layui-input-inline">
                    <select name="type" id="type" lay-filter="type" lay-search>
                        <option value="">选择招聘类别</option>
                        <option value="1">实习招聘</option>
                        <option value="2">就业招聘</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择院校</label>
                <div class="layui-input-inline">
                    <select name="universityid" lay-filter="university" lay-search>
                        <option value=""选择学校></option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="schoolid" lay-filter="school" lay-search>
                        <option value="">选择学院</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="positiontype" lay-filter="positionType" lay-search>
                        <option value="">选择岗位</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">工作城市</label>
                <div class="layui-input-inline">
                    <input name="workingcity" id="workingcity" class="layui-input">
                </div>
                <label class="layui-form-label">起薪</label>
                <div class="layui-input-inline">
                    <input name="salaryllimit" id="salaryllimit" class="layui-input" lay-verify="numOfSalary">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">工作内容</label>
                <div class="layui-input-block">
                    <textarea id="ta_ckeditor" class="ckeditor my-math" cols="20" rows="2"
                              style="overflow:auto"></textarea>
                    <script type="text/javascript">CKEDITOR.replace('ta_ckeditor');</script>
                </div>
                <%--<textarea id="ta_ckeditor" class="ckeditor my-math" cols="20" rows="2" style="overflow:auto"></textarea>--%>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">工作环境照片</label>
                <div class="layui-input-block">
                    <img src="" id="showWorkingPic" style="height: 100px;width: 100px">
                    <input type="file" name="fileUp" id="workingpic" class="layui-upload-file">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">其他说明</label>
                <div class="layui-input-block">
                    <input name="comment" id="comment" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">招聘人数</label>
                <div class="layui-input-inline">
                    <input name="recruitnum" id="recruitnum" class="layui-input" lay-verify="numOfPerson">
                </div>
                <label class="layui-form-label">联系人</label>
                <div class="layui-input-inline">
                    <input name="contactperson" id="contactperson" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-inline">
                    <input name="phone" id="phone" class="layui-input">
                </div>
                <label class="layui-form-label">联系邮箱</label>
                <div class="layui-input-inline">
                    <input name="email" id="email" class="layui-input">
                </div>
            </div>
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="edit">提交</button>
                <button type="submit" class="layui-btn" lay-submit lay-filter="save">暂存</button>
                <button type="submit" class="layui-btn" lay-submit lay-filter="reload">返回</button>
            </div>
        </form>
    </form>
</div>
<script src="../../static/common/layui/layui.js"></script>
<script>
    var myDate = new Date();
    var recruitInfoId = <%=request.getParameter("recruitInfoId")%>;
    var recruitInfo = {};
    var schoolInfo = {};
    var workingPicture = {};
    axios.get('/recruitInfo/find/'+recruitInfoId)
        .then(function (response) {
            console.log(response.data);
            recruitInfo = response.data.data.recruitInfo;
            schoolInfo = response.data.data.school;
            workingPicture = response.data.data.workingPic;
            $('#title').val(recruitInfo.title);
            $('#type').val(recruitInfo.type);
            $('#workingcity').val(recruitInfo.workingcity);
            $('#salaryllimit').val(recruitInfo.salaryllimit);
            CKEDITOR.instances.ta_ckeditor.setData(recruitInfo.job);
            $('#comment').val(recruitInfo.comment);
            $('#recruitnum').val(recruitInfo.recruitnum);
            $('#contactperson').val(recruitInfo.contactperson);
            $('#phone').val(recruitInfo.phone);
            $('#email').val(recruitInfo.email);
            if(workingPic != null) {
                $('#showWorkingPic').attr('src', workingPicture.path);
            }
//            if(recruitInfo.workingpic !== 0 && recruitInfo.workingpic !== null) {
//                axios.get('/image/find/' + recruitInfo.workingpic)
//                    .then(function (res) {
//                        console.log(res);
//                        $('#showWorkingPic').attr('src',res.data.data);
//                    })
//                    .catch(function (msg) {
//                        console.log(msg);
//                    })
//            }
        })
        .catch(function (msg) {
            console.log(msg);
        })
</script>
<script>
    layui.use(['form', 'jquery', 'layer', 'upload'], function () {
        var form = layui.form(),
            $ = layui.jquery;

        var image = {};
        //联动对象
        var linkage = {};
        //渲染下拉框
        linkage.formRender = function (obj, tip) {
            $(obj).empty();
            $(obj).append('<option value="">' + tip + '</option>');
            form.render('select');
        };
        // 初始数据不存在
        linkage.readNoData = function (obj,url) {
            axios.get(url)
                .then(function (res) {
                    console.log(res.data);
                    var array = res.data.data;
                    var html = '';
                    for (var i in array) {
                        html += '<option value=' + array[i].id + '>' + array[i].name + '</option>';
                    }
                    $(obj).append(html);
                    form.render('select');
                })
                .catch(function (res) {
                    console.log(res);
                });
        };
        //读取数据库信息并写回到select框中（有数据已存在，可进行更改）
        linkage.readInit = function (obj, url, init,id) {
            axios.get(url)
                .then(function (res) {
                    var array = res.data.data;
                    var html = '';
                    for (var i in array) {
                        html += '<option value=' + array[i].id + '>' + array[i].name + '</option>';
                    }
                    $(obj).append(html);
                    form.render('select');
                    // console.log(init);
                    if (init)
                        $(obj).next().find('[lay-value="' + id + '"]').click();
                })
                .catch(function (res) {
                    console.log(res);
                });
        };
        // 渲染岗位类别
        linkage.readPosition = function (obj,url) {
            axios.get(url)
                .then(function (res) {
                    console.log(res.data);
                    var array = res.data.data;
                    var html = '';
                    for (var i in array) {
                        html += '<option value=' + array[i].id + '>' + array[i].posname + '</option>';
                    }
                    $(obj).append(html);
                    form.render('select');
                })
                .catch(function (res) {
                    console.log(res);
                });
        };
        linkage.readPositionInit = function (obj, url, init,id) {
            axios.get(url)
                .then(function (res) {
                    var array = res.data.data;
                    var html = '';
                    for (var i in array) {
                        html += '<option value=' + array[i].id + '>' + array[i].posname + '</option>';
                    }
                    $(obj).append(html);
                    form.render('select');
                    // console.log(init);
                    if (init)
                        $(obj).next().find('[lay-value="' + id + '"]').click();
                })
                .catch(function (res) {
                    console.log(res);
                });
        };

        var university = 'form select[name=universityid]',
            school = 'form select[name=schoolid]',
            positiontype = 'form select[name=positiontype]';
        //读取大学
        if ($(university).length) {
            linkage.formRender(university, "选择大学");
            if(recruitInfo != null){
                console.log(school);
                linkage.readInit(university,'/university/findAll',schoolInfo,schoolInfo.universityid);
            }
            else {
                linkage.readNoData(university, '/university/findAll');
            }
            //读取学院
            if ($(school).length) {
                linkage.formRender(school, "选择学院");
                form.on('select(university)', function (data) {
                    linkage.formRender(school, "选择学院");
                    linkage.formRender(positiontype, "选择岗位");
                    if(recruitInfo != null){
                        linkage.readInit(school,'/school/findBath/'+data.value,schoolInfo,schoolInfo.id);
                    }
                    else {
                        linkage.readNoData(school, '/school/findBath/' + data.value);
                    }
                });
                if($(positiontype).length){
                    linkage.formRender(positiontype, "选择岗位");
                    form.on('select(school)',function (data) {
                        console.log(data);
                        recruitInfo.schoolid = data.value;
                        linkage.formRender(positiontype, "选择岗位");
                        if(recruitInfo.positiontype != null){
                            linkage.readPositionInit(positiontype,'/PositionType/findBatchBySchoolId/' + data.value,recruitInfo.positiontype,recruitInfo.positiontype);
                        }
                        else {
                            linkage.readPosition(positiontype, '/PositionType/findBatchBySchoolId/' + data.value);
                        }
                    })
                }
            }
        }
        //首先渲染
        form.render();
        var workingpic;
        var da = new Date();
        var myDate = da.getFullYear() + "-" + (da.getMonth() + 1) + "-" + da.getDate() + " " + da.getHours() + ":" + da.getMinutes() + ":" + da.getSeconds();

        form.on('submit(edit)', function (data) {
            console.log(data.field);
            data.field.publisherid = ${sessionScope.currentUser.getId()};
            data.field.job = CKEDITOR.instances.ta_ckeditor.getData();
            data.field.publishdate = myDate;
            data.field.state = 2;
            data.field.workingpic = workingpic;
            data.field.orgid =orgId;
            if(recruitInfoId != null){
                data.field.id = recruitInfoId;
            }
            $.ajax({
                url: recruitInfoId == null ? '/recruitInfo/add':'/recruitInfo/modify'
                , type: 'post'
                , contentType: "application/json; charset=utf-8"
                , data: JSON.stringify(data.field)
                , dataType: 'json'
                , success: function (response) {
                    layer.msg(response.message, function () {
                        window.history.go(-1);
//                        parent.tab.deleteTab(parent.tab.getCurrentTabId());
                    });
                }
            });
            return false;
        });
        form.on('submit(save)', function (data) {
            console.log(data.field);
            data.field.publisherid = ${sessionScope.currentUser.getId()};
            data.field.job = CKEDITOR.instances.ta_ckeditor.getData();
            data.field.publishdate = myDate;
            data.field.state = 1;
            data.field.workingpic = workingpic;
            data.field.orgid =orgId;
            if(recruitInfoId != null){
                data.field.id = recruitInfoId;
            }
            $.ajax({
                url: recruitInfoId == null ? '/recruitInfo/add':'/recruitInfo/modify'
                , type: 'post'
                , contentType: "application/json; charset=utf-8"
                , data: JSON.stringify(data.field)
                , dataType: 'json'
                , success: function (response) {
                    layer.msg(response.message, function () {
                        window.history.go(-1);
                    },{time:1000});
                }
            });
            return false;
        });
        form.on('submit(reload)', function (data) {
            window.history.go(-1);
        });

        form.verify({
            numOfSalary:function (data) {
                if(isNaN(data)&&data!=''){
                    layer.msg("起薪必须为数字",{icon:2,time:1000});
                }
            },
            numOfPerson:function (data) {
                if(isNaN(data)&&data!=''){
                    layer.msg("招聘人数必须为数字",{icon:2,time:1000});
                }
            }
        });


        layui.upload({
            url: '/upload/workingpic',
            success: function (response) {
                image.path = response.data;
                image.description = "工作环境";
                image.isencrypted = 2;//需要加密
                axios.post("/image/add", image)
                    .then(function (response) {
                        console.log(response.data);
                        axios.get("/image/find/" + response.data.data)
                            .then(function (response) {
                                console.log(response);
                                $('#showWorkingPic').attr('src', response.data.data);
                            })
                            .catch(function (response) {
                                console.log(response);
                            });
                    })
                    .catch(function (response) {
                        console.log(response);
                    })

            },
            before: function () {
                console.log('工作环境上传中');
            }
        });
    })
</script>
</body>
</html>
